import Toast from '../../component/vant/toast/toast';
import $wuxCountDown from '../../component/wux/countdown/index';
const util = require('../../utils/util.js');
import Dialog from '../../component/vant/dialog/dialog';
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    my_course: [],
    status: 0,
    domain: app.api.xh.downloadDomain,
    teacherTagStyle: util.teacherTagStyles,
    loaded: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let page = this;
    var logging = wx.getStorageSync('_logining');

    if (!logging) {
      app.toAuth('/pages/my/my');
    } else {
      // this.getOrderList();
    }

    let currentTab = wx.getStorageSync('currentOrderTab');
    if (currentTab) {
      page.setData({
        currentTab: currentTab
      });
    }

    // this.time = new $wuxCountDown({
    //   date: +(new Date) + 60000 * 15,
    //   render(date) {
    //       const min = this.leadingZeros(date.min, 2) + ':';
    //       const sec = this.leadingZeros(date.sec, 2) + '';
    //       this.setData({
    //         time: min + sec
    //       });
    //   }
    // });

    if (options.status != undefined) {
      page.setData({
        currentTab: options.status
      });
    }
  },
  onChange(e) {
    this.setData({
      currentTab: e.detail.index
    });
    wx.setStorageSync('currentOrderTab', e.detail.index);
    app.goTop();
  },
  //  我的订单
  getOrderList: function() {
    var page = this;
    app.sendRequest({
      hideLoading: true,
      url: app.api.order.list,
      data: {
        status: page.data.status
      },
      success: function(res) {
        if (res.data != undefined) {
          page.setData({
            my_order_all: res.data.list
          });
          page.initData();
        }
      }
    });
  },
  initData() {
    let page = this,
      my_order = page.data.my_order_all || [],
      all_order = [],
      uncharge_order = [],
      charged_order = [],
      invalid_order = [],
      canceled_order = [];
    my_order.map(order => {
      let total_fee = (order.total_fee / 100).toFixed(2);
      order.total_fee = total_fee;
      all_order.push(order);
      // 未支付
      if (order.status == 1) {
        uncharge_order.push(order);
      } else if (order.status == 2) {
        //   已支付
        charged_order.push(order);
      } else if (order.status == 3) {
        //   已失效
        invalid_order.push(order);
      } else if (order.status == 4) {
        //   已取消
        canceled_order.push(order);
      }
    });
    page.setData({
      loaded: 1,
      all_order: all_order,
      uncharge_order: uncharge_order,
      charged_order: charged_order,
      invalid_order: invalid_order,
      canceled_order: canceled_order
    });
  },
  toOrder: function(e) {
    var out_trade_no = e.currentTarget.dataset.id;
    var status = e.currentTarget.dataset.status;
    var device = e.currentTarget.dataset.device;
    wx.navigateTo({
      url:
        '/pages/sure_buy/sure_buy?out_trade_no=' +
        out_trade_no +
        '&status=' +
        status +
        '&device=' +
        device
    });
  },
  delOrder(e) {
    let page = this,
      out_order_no = e.currentTarget.dataset.id;

    Dialog.confirm({
      title: '提示',
      message: '确定删除订单吗'
    })
      .then(() => {
        // 删除订单
        app.sendRequest({
          url: app.api.order.option,
          data: {
            out_trade_no: out_order_no,
            status: 5
          },
          method: 'POST',
          header: {
            token: wx.getStorageSync('access_token'),
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function(res) {
            Toast({
              duration: 500,
              message: '删除成功！',
              onClose: function name(params) {
                wx.redirectTo({
                  url: '/pages/my_order/my_order'
                });
              }
            });
          }
        });
      })
      .catch(() => {
        // on cancel
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var logging = wx.getStorageSync('_logining');

    if (logging) {
      this.getOrderList();
    }

    let page = this;
    wx.getSystemInfo({
      success: function(res) {
        let scrollViewHeight = page.data.isIPX
          ? res.windowHeight - 118
          : res.windowHeight - 81;
        page.setData({
          scrollViewHeight: scrollViewHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getOrderList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
